home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Language/OS - Multiplatform Resource Library
/
LANGUAGE OS.iso
/
clean
/
sun3.lha
/
Sun3
/
pardemos
/
parqs.icl
next >
Wrap
Text File
|
1992-08-07
|
4KB
|
117 lines
MODULE parqs;
<<
Parallel quicksort.
This program sorts a list of integers using quicksort. When the lists
are bigger than a certain threshold during the sorting process, the
sorting will be done using divide and conquer parallellism. Otherwise
a sequential version of the quicksort algorithm will be called.
The result of the program is a sorted list of integers.
Run the program with the show constructors option on.
>>
IMPORT deltaI;
TYPE
<<
For the parallel quicksort algorithm it is more convenient to use
a tree to represent the result. The sequential version uses lists
to represent the result, so the result of the sorting process will be
a binary tree filled with integers of which the leafs are lists of
integers of size < Threshold. At the end of the sorting process this
tree will be transformed into a list again.
>>
:: SortResult x -> Tree x (SortResult x) (SortResult x)
-> Leaf [x];
MACRO
Threshold -> 7; == Lists of length >= Threshold will be sorted in parallel
RULE
== The list that will be sorted.
:: UnsortedList -> [INT];
UnsortedList -> [1,3,6,4,2,7,3,12,5,17,4,987,3,2,17,4,5,6,93,5,28,
16,4,11,89,1,30,12,78,35,27,85,7,48,63,21,7,8,2,
10,7,20,832,92,111,67,21,8438,12,1,26,53,51,
114,12,23,68,38,1099,0,12,-12,7,3,15,101,23,4,
77,1,3,5,3,1,67,123,1,-9,0,7,4,53,52,51,31];
<<
Miscellaneous list functions.
>>
== Is the lenght of the list smaller than n?
:: Smaller [x] INT -> BOOL;
Smaller [] n -> TRUE;
Smaller [f|r] 0 -> FALSE;
Smaller [f|r] n -> Smaller r (-- n);
== FastAppend is fast because of the local !-annotation.
:: FastAppend [x] [x] -> [x];
FastAppend [] rest -> rest;
FastAppend [f|r] rest ->[f | !FastAppend r rest];
<<
The parallel quicksort algorithm.
Sorter sorts a list of integers and returns a tree containing the
sorted result. If the incoming list is smaller than Threshold the
list will be sorted sequentially by SeqSorter whose result will
be stored in a leaf of the tree. Otherwise the parallel algorithm
(ParSorter) will be called, which will result in a binary tree of
processes of which the rootnodes represent a value, the left branch
is a process sorting the values smaller than the rootvalue and the
right branch is a process sorting the values greater than the rootvalue.
>>
:: Sorter [INT] -> SortResult INT;
Sorter list -> Leaf (SeqSorter list) , IF Smaller list Threshold
-> ParSorter list;
:: ParSorter [INT] -> SortResult INT;
ParSorter [f|r] -> Tree f left right,
left: {P} Sorter small,
right: {P} Sorter large,
small: {I} Filter r (>= f),
large: {I} Filter r (< f);
:: Filter [x] (=> x BOOL) -> [x];
Filter [] op -> [];
Filter [f|r] op -> [f | {I} Filter r op] , IF op f
-> Filter r op;
:: SeqSorter [INT] -> [INT];
SeqSorter [] -> [];
SeqSorter [f|r] -> FastAppend (SeqSorter small) [f|SeqSorter large],
(small,large): Split f r [] [];
:: Split INT [INT] [INT] [INT] -> ([INT],[INT]);
Split el [] left right -> (left,right);
Split el [f|r] left right -> Split el r [f|left] right , IF < f el
-> Split el r left [f|right];
== WalkTree transforms the tree that is the result of the sorting process
== into a sorted list of integers.
:: WalkTree (SortResult x) -> [x];
WalkTree (Leaf list) -> list;
WalkTree (Tree f l r) -> FastAppend (WalkTree l) [f|WalkTree r];
== The Start rule: Sort the unsorted list and transform the result into a list.
:: Start -> [INT];
Start -> WalkTree sortresult,
sortresult: Sorter list,
list: UnsortedList;